package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.ShiftDto;
import com.yc.sole.erp.entity.Shift;
import com.yc.sole.erp.mapper.ShiftMapper;
import com.yc.sole.erp.request.ShiftQueryReq;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-25
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ShiftRepository extends ServiceImpl<ShiftMapper, Shift> {

    /**
     * 新增
     *
     * @param shiftDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveShift(ShiftDto shiftDto) {
        Shift entity = new Shift();
        BeanUtil.copyProperties(shiftDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param shiftDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateShiftById(ShiftDto shiftDto) {
        Shift entity = new Shift();
        BeanUtil.copyProperties(shiftDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteShiftByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<ShiftDto> selectShiftByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), ShiftDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public ShiftDto selectShiftById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), ShiftDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<ShiftDto> selectShiftPage(Paging page, ShiftQueryReq shiftQueryReq) {
        LambdaQueryWrapper<Shift> queryWrapper = Wrappers.<Shift>lambdaQuery();
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), ShiftDto.class);
    }

    /**
     * 根据名称查询
     *
     * @return
     */
    public List<ShiftDto> selectAllShift() {
        LambdaQueryWrapper<Shift> queryWrapper = Wrappers.<Shift>lambdaQuery().eq(Shift::getState, CommonState.ENABLE);
        return BeanUtil.copyToList(this.list(queryWrapper), ShiftDto.class);
    }
}
